package com.itheima.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.commen.Code;
import com.itheima.commen.PageResult;
import com.itheima.commen.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    //注入userService
    @Autowired
    private UserService userService;

    /**
     * 分页查询所有
     *
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result<User>  getAll(@PathVariable("page") int page, @PathVariable("size") int size) {
//        int i = 1/0;
//        if (true) throw new BusinessException(new Result(Code.ERROR,"您电脑该换了"));

        PageHelper.startPage(page,size);
        PageInfo<User> userList = userService.getAll(page,size);
        PageResult pageResult = new PageResult(userList.getTotal(),userList.getPages(),userList.getList());

        return new Result(Code.OK,pageResult,"查询成功");
    }

    /**
     * 查询单个
     *
     * @return
     */
    @GetMapping("/{id}")
    public Result<User> get(@PathVariable("id") int id) {
        User user = userService.get(id);
        return new Result<User>(user!=null?Code.OK:Code.ERROR,user,user!=null?"查询成功":"查询失败");
    }

    /**
     * 新增操作
     *
     * @param user
     * @return
     */
    @PostMapping
    public Result<User> save(@RequestBody User user) {
        boolean b = userService.save(user);
        return new Result<User>(b?Code.OK:Code.ERROR,b?"新增成功":"新增失败");
    }

    /**
     * 批量删除
     *
     * @param uuids
     * @return
     */
    @DeleteMapping("/{uuids}")
    public Result<User> deleteByUuids(@PathVariable("uuids") String uuids) {

        boolean b = userService.deleteByUuids(uuids);
        return new Result<User>(b?Code.OK:Code.ERROR,b?"删除成功":"删除失败");

    }

    /**
     * 修改操作
     *
     * @param user
     * @return
     */
    @PutMapping
    public Result<User> update(@RequestBody User user) {
        boolean b = userService.update(user);
        return new Result<User>(b?Code.OK:Code.ERROR,b?"修改成功":"修改失败");
    }

    /**
     * 登录验证
     *
     * @param userName
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Result<User> login(String userName,String password) {

        User user = userService.login(userName, password);
        return new Result<User>(user!=null?Code.OK:Code.ERROR,user,user!=null?"查询成功":"查询失败");
    }

    /**
     * 分页条件查询
     *
     * @param user
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<User> getUsers(@RequestBody User user,@PathVariable("page") int page,@PathVariable("size") int size) {
        PageInfo<User> users = userService.getUsers(user, page, size);
        PageResult pageResult = new PageResult(users.getTotal(),users.getPages(),users.getList());

        return new Result(Code.OK,pageResult,"查询成功");
    }
}
